<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/18
 * Time: 14:41
 */
namespace app\business\controller;
use think\Db;
class Timing extends Base {
    /**
     * 超过7天把送的券再返回来等等（1天一跑）
     */
    public function selfshop(){
        set_time_limit(0);
        //定时器1：解答超过1小时未提交则清除状态；定时器3：未答题的一小时后关闭
        //1-1：超过7天把送的券再返回来
        $today = date('Y-m-d H:i:s');
        $time = time();
        $one_hour_ago = date("Y-m-d",($time - 3600*24*7));
        $where = array();
//        $where['mc_status'] = 1;
        $where['mc_send'] = 2;
        $where['mc_send_status'] = 2;
        $where['mc_send_time'] = ['<=',$one_hour_ago];
        Db::table('up_my_shop_coupon')
            ->where($where)
            ->update([
                'mc_send' => 1,
                'mc_send_status' => 4,
                'mc_take_time' => $today
            ]);
        //1-2：商家优惠券过期
        $today = date("Y-m-d");
        $where = array();
        $where['coupon_status'] = 1;
        $where['coupon_end'] = ['<',$today];
        $end_cs = Db::table('up_shop_coupon')
            ->where($where)
            ->field('coupon_num,coupon_get_num,coupon_price,coupon_s_id,coupon_name')
            ->select();
        if($end_cs){
            Db::table('up_shop_coupon')
                ->where($where)
                ->update([
                    'coupon_status' => 2
                ]);
            foreach ($end_cs as $k=>$v){
                $back = ($v['coupon_num'] - $v['coupon_get_num']) * $v['coupon_price'];
                if ($back > 0) {
                    Db::name('shops')
                        ->where('s_id', $v['coupon_s_id'])
                        ->setInc('s_money', $back);
                    //入商户明细和商户消息
                    allInShopDetail($back, 2, $v['coupon_s_id'], $v['coupon_name'] . '优惠券到期退还',8);
                    allInNotice('优惠券到期退还', $v['coupon_name'] . '优惠券到期退还，返还' . ($back / 10) . '元！', 3, $v['coupon_s_id']);
                }
            }
        }

        //1-3：用户领取后超时不用，过期
        $where = array();
        $where['mc_status'] = 1;
        $where['mc_over_date'] = ['<',$today];
        Db::table('up_my_shop_coupon')
            ->where($where)
            ->update([
                'mc_status' => 3
            ]);

    }

    public function dowload() {//echo 1;die;
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
           // echo "<script> location.href='http://openbox.mobilem.360.cn/index/d/sid/4159000'; </script>";
            echo '学生端 ios 下载地址';
        }else{
           echo "<script> location.href='http://openbox.mobilem.360.cn/index/d/sid/4159000'; </script>";
        }

    }
}